package com.huixuebao.exchange.service.Impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.huixuebao.exchange.entity.TempUserEntity;
import com.huixuebao.exchange.mapper.TempUserMapper;
import com.huixuebao.exchange.service.TempUserService;
import com.huixuebao.exchange.tempDto.TempUserDTO;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import com.huixuebao.common.util.ConvertUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class TempUserServiceImpl implements TempUserService {

    @Autowired
    TempUserMapper tempUserMapper;

    @Override
    public ApiResult addTempService(TempUserDTO tempUserDTO) {
            ApiResult apiResult = new ApiResult();
            try{
                String userName = tempUserDTO.getUserName();
                String userMobile = tempUserDTO.getUserMobile();
                String policyCode = tempUserDTO.getPolicyCode();
                if(StrUtil.isNotEmpty(userName) && StrUtil.isNotEmpty(userMobile)) {
                    TempUserEntity tempUserEntity = new TempUserEntity();
                    tempUserEntity.setUserName(userName);
                    tempUserEntity.setUserMobile(userMobile);
                    tempUserEntity.setPolicyCode(policyCode);
                    tempUserEntity.setCreateTime(new DateTime());
                    tempUserEntity.setId(IdUtil.fastSimpleUUID());
                    tempUserMapper.insert(tempUserEntity);
                    apiResult.setSuccess(true);
                    apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                    apiResult.setMessage("添加临时用户信息成功！！");
                }
            }catch (Exception e){
                log.info("添加临时用户信息失败!!");
                apiResult.setSuccess(false);
                apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
                apiResult.setMessage("添加临时用户信息失败！！");
            }
            return apiResult;
    }

    @Override
    public ApiResult getTempUserList(Integer current,Integer size,String userName,String userMobile,String policyCode) {
        ApiResult apiResult = new ApiResult();
        try {
            if (ObjectUtil.isNotNull(current) && ObjectUtil.isNotNull(size)) {
                IPage<TempUserEntity> page = new Page<>(current, size);
                IPage<TempUserEntity> entityIPage = tempUserMapper.selectPage(page, new LambdaQueryWrapper<TempUserEntity>().orderByDesc(TempUserEntity::getCreateTime)
                        .like(StrUtil.isNotEmpty(userName),TempUserEntity::getUserName,userName)
                        .like(StrUtil.isNotEmpty(userMobile),TempUserEntity::getUserMobile,userMobile)
                        .like(StrUtil.isNotEmpty(policyCode),TempUserEntity::getPolicyCode,policyCode));
                IPage<TempUserDTO> doList = ConvertUtils.convertToDTOPage(entityIPage, TempUserDTO.class);
                apiResult.setData(doList);
                apiResult.setSuccess(true);
                apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                apiResult.setMessage("获取临时用户分页信息成功！！");
            }else {
                List<TempUserEntity> list = tempUserMapper.selectList(new LambdaQueryWrapper<TempUserEntity>().orderByDesc(TempUserEntity::getCreateTime)
                        .like(StrUtil.isNotBlank(userName), TempUserEntity::getUserName, userName)
                        .like(StrUtil.isNotBlank(userMobile), TempUserEntity::getUserMobile, userMobile)
                        .like(StrUtil.isNotBlank(policyCode), TempUserEntity::getPolicyCode, policyCode));
                List<TempUserDTO> doList = ConvertUtils.convertToDTOList(list, TempUserDTO.class);
                apiResult.setData(doList);
                apiResult.setSuccess(true);
                apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                apiResult.setMessage("获取临时用户信息失败!!");
            }
        } catch (Exception e) {
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("获取临时用户信息失败！！");
        }
        return apiResult;
    }
}
